<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      class="formWidth"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="军人类别" prop="soldierType">
        <el-select
          v-model="queryParams.soldierType"
          placeholder="请选择军人类别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in soldierTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="身体状况" prop="physicalCondition">
        <el-select
          v-model="queryParams.physicalCondition"
          placeholder="请选择身体状况"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in physicalConditionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优抚类别" prop="disabilityType">
        <el-select
          v-model="queryParams.disabilityType"
          placeholder="请选择优抚类别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in disabilityTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="残疾两项补" prop="isSubsidy">
        <el-select
          v-model="queryParams.isSubsidy"
          placeholder="请选择是否享受残疾人两项补"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in isSubsidyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="伤残等级" prop="disabilityLevel">
        <el-select
          v-model="queryParams.disabilityLevel"
          placeholder="请选择伤残等级"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in disabilityLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="伤残性质" prop="disabilityProperty">
        <el-select
          v-model="queryParams.disabilityProperty"
          placeholder="请选择伤残性质"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in disabilityPropertyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="伤残证件名" prop="certificateName">
        <el-select
          v-model="queryParams.certificateName"
          placeholder="请选择伤残证件名称"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in certificateNameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="伤残证件号" prop="certificateNo">
        <el-input
          v-model="queryParams.certificateNo"
          placeholder="请输入伤残证件号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="立功受奖" prop="honour">
        <el-select
          v-model="queryParams.honour"
          placeholder="请选择立功受奖情况"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in honourOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="立功受奖证件 上传的地址" prop="honourCertificate">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.honourCertificate"-->
      <!--          placeholder="请输入立功受奖证件 上传的地址"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:retirefm_1:add']"
        >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:retirefm_1:edit']"
        >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:retirefm_1:remove']"
        >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['manager:retirefm_1:export']"
        >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="retirefm_1List"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" :show-overflow-tooltip="true" align="center" prop="soldierName" />
      <el-table-column
        label="军人类别"
        align="center"
        prop="soldierType"
        :show-overflow-tooltip="true"
        :formatter="soldierTypeFormat"
      />
      <el-table-column
        label="身体状况"
        align="center"
        prop="physicalCondition"
        :show-overflow-tooltip="true"
        :formatter="physicalConditionFormat"
      />
      <el-table-column
        label="优抚类别"
        align="center"
        prop="disabilityType"
        :show-overflow-tooltip="true"
        :formatter="disabilityTypeFormat"
      />
      <el-table-column
        label="残疾两项补"
        align="center"
        prop="isSubsidy"
        :show-overflow-tooltip="true"
        :formatter="isSubsidyFormat"
      />
      <el-table-column
        label="伤残等级"
        align="center"
        prop="disabilityLevel"
        :show-overflow-tooltip="true"
        :formatter="disabilityLevelFormat"
      />
      <el-table-column
        label="伤残性质"
        align="center"
        prop="disabilityProperty"
        :show-overflow-tooltip="true"
        :formatter="disabilityPropertyFormat"
      />
      <el-table-column
        label="伤残证件名"
        align="center"
        prop="certificateName"
        :show-overflow-tooltip="true"
        :formatter="certificateNameFormat"
      />
      <el-table-column
        label="伤残证件号"
        align="center"
        :show-overflow-tooltip="true"
        prop="certificateNo"
      />
      <el-table-column
        label="立功受奖"
        align="center"
        prop="honour"
        :show-overflow-tooltip="true"
        :formatter="honourFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:retirefm_1:edit']"
          >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:retirefm_1:remove']"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退役军人_优抚信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" class="formWidth" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20" >
          <el-col :span="12" >
            <el-form-item label="身份证号" prop="soldierId" v-if="!isUpdate">
              <el-select
                v-model="form.soldierId"
                filterable
                remote
                reserve-keyword
                placeholder="输入身份证号系统自动匹配"
                :remote-method="remoteMethod"
                :loading="idLoading"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="军人类别" prop="soldierType">
              <el-select v-model="form.soldierType" placeholder="请选择军人类别">
                <el-option
                  v-for="dict in soldierTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->

<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="身体状况" prop="physicalCondition">
              <el-select
                v-model="form.physicalCondition"
                placeholder="请选择身体状况"
              >
                <el-option
                  v-for="dict in physicalConditionOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="优抚类别" prop="disabilityType">
              <el-select v-model="form.disabilityType" placeholder="请选择优抚类别">
                <el-option
                  v-for="dict in disabilityTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->

<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="残疾两项补" prop="isSubsidy">
              <el-select
                v-model="form.isSubsidy"
                placeholder="请选择是否享受残疾人两项补"
              >
                <el-option
                  v-for="dict in isSubsidyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="伤残等级" prop="">
              <el-select
                v-model="form.disabilityLevel"
                placeholder="请选择伤残等级"
              >
                <el-option
                  v-for="dict in disabilityLevelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->

<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="伤残性质" prop="">
              <el-select
                v-model="form.disabilityProperty"
                placeholder="请选择伤残性质"
              >
                <el-option
                  v-for="dict in disabilityPropertyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="伤残证件名称" prop="">
              <el-select
                v-model="form.certificateName"
                placeholder="请选择伤残证件名称"
              >
                <el-option
                  v-for="dict in certificateNameOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->

<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="伤残证件号码" prop="">
              <el-input
                v-model="form.certificateNo"
                placeholder="请输入伤残证件号码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="立功受奖情况" prop="honour">
              <el-select v-model="form.honour" placeholder="请选择立功受奖情况">
                <el-option
                  v-for="dict in honourOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-form-item label="立功受奖证件 上传的地址" prop="honourCertificate">
          <el-input
            v-model="form.honourCertificate"
            placeholder="请输入立功受奖证件 上传的地址"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRetirefm_1,
  getRetirefm_1,
  delRetirefm_1,
  addRetirefm_1,
  updateRetirefm_1,
  exportRetirefm_1,
  getBaseInfoByID,
} from "@/api/care/care_retirefm_1";

export default {
  name: "Retirefm_1",
  data() {
    return {
      idLoading: false,
      // 身份证列表
      options: [],
      isUpdate:false,

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退役军人_优抚信息表格数据
      retirefm_1List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 军人类别字典
      soldierTypeOptions: [],
      // 身体状况字典
      physicalConditionOptions: [],
      // 优抚类别字典
      disabilityTypeOptions: [],
      // 是否享受残疾人两项补字典
      isSubsidyOptions: [],
      // 伤残等级字典
      disabilityLevelOptions: [],
      // 伤残性质字典
      disabilityPropertyOptions: [],
      // 伤残证件名称字典
      certificateNameOptions: [],
      // 立功受奖情况字典
      honourOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soldierType: null,
        physicalCondition: null,
        disabilityType: null,
        isSubsidy: null,
        disabilityLevel: null,
        disabilityProperty: null,
        certificateName: null,
        certificateNo: null,
        honour: null,
        honourCertificate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        soldierId: [
          {
            required: true,
            message: "军人身份证号不能为空",
            trigger: "blur",
          },
        ],
        soldierType: [
          { required: true, message: "军人类别不能为空", trigger: "change" },
        ],
        physicalCondition: [
          { required: true, message: "身体状况不能为空", trigger: "change" },
        ],
        disabilityType: [
          { required: true, message: "优抚类别不能为空", trigger: "change" },
        ],
        isSubsidy: [
          {
            required: true,
            message: "是否享受残疾人两项补不能为空",
            trigger: "change",
          },
        ],
        disabilityLevel: [
          { required: true, message: "伤残等级不能为空", trigger: "change" },
        ],
        disabilityProperty: [
          { required: true, message: "伤残性质不能为空", trigger: "change" },
        ],
        certificateName: [
          {
            required: true,
            message: "伤残证件名称不能为空",
            trigger: "change",
          },
        ],
        certificateNo: [
          { required: true, message: "伤残证件号码不能为空", trigger: "blur" },
        ],
        honour: [
          {
            required: true,
            message: "立功受奖情况不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("soldier_type").then((response) => {
      this.soldierTypeOptions = response.data;
    });
    this.getDicts("physical_condition").then((response) => {
      this.physicalConditionOptions = response.data;
    });
    this.getDicts("disability_type").then((response) => {
      this.disabilityTypeOptions = response.data;
    });
    this.getDicts("yes_or_no").then((response) => {
      this.isSubsidyOptions = response.data;
    });
    this.getDicts("disability_level").then((response) => {
      this.disabilityLevelOptions = response.data;
    });
    this.getDicts("disability_property").then((response) => {
      this.disabilityPropertyOptions = response.data;
    });
    this.getDicts("certificate_name").then((response) => {
      this.certificateNameOptions = response.data;
    });
    this.getDicts("honour").then((response) => {
      this.honourOptions = response.data;
    });
  },
  methods: {
    // 通过身份证查询基本信息
    remoteMethod(id) {
      if (id != "") {
        let params = {
          idNumber: id,
        };
        this.options = [];
        getBaseInfoByID(params)
          .then((res) => {
            if (res.rows == null || res.rows == undefined) {
              this.options = [];
              return;
            }
            this.options = res.rows.map((item) => {
              return {
                value: item.id,
                label: item.idNumber,
              };
            });
          })
          .catch((err) => console.error(err));
      } else {
        this.options = [];
      }
    },
    /** 查询退役军人_优抚信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.segment = "4";
      listRetirefm_1(this.queryParams).then((response) => {
        this.retirefm_1List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 军人类别字典翻译
    soldierTypeFormat(row, column) {
      return this.selectDictLabel(this.soldierTypeOptions, row.soldierType);
    },
    // 身体状况字典翻译
    physicalConditionFormat(row, column) {
      return this.selectDictLabel(
        this.physicalConditionOptions,
        row.physicalCondition
      );
    },
    // 优抚类别字典翻译
    disabilityTypeFormat(row, column) {
      return this.selectDictLabel(
        this.disabilityTypeOptions,
        row.disabilityType
      );
    },
    // 是否享受残疾人两项补字典翻译
    isSubsidyFormat(row, column) {
      return this.selectDictLabel(this.isSubsidyOptions, row.isSubsidy);
    },
    // 伤残等级字典翻译
    disabilityLevelFormat(row, column) {
      return this.selectDictLabel(
        this.disabilityLevelOptions,
        row.disabilityLevel
      );
    },
    // 伤残性质字典翻译
    disabilityPropertyFormat(row, column) {
      return this.selectDictLabel(
        this.disabilityPropertyOptions,
        row.disabilityProperty
      );
    },
    // 伤残证件名称字典翻译
    certificateNameFormat(row, column) {
      return this.selectDictLabel(
        this.certificateNameOptions,
        row.certificateName
      );
    },
    // 立功受奖情况字典翻译
    honourFormat(row, column) {
      return this.selectDictLabel(this.honourOptions, row.honour);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        soldierId: null,
        soldierType: null,
        physicalCondition: null,
        disabilityType: null,
        isSubsidy: null,
        disabilityLevel: null,
        disabilityProperty: null,
        certificateName: null,
        certificateNo: null,
        honour: null,
        honourCertificate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isUpdate = false;
      this.reset();
      this.open = true;
      this.title = "添加退役军人_优抚信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isUpdate = true;
      this.reset();
      const id = row.id || this.ids;
      getRetirefm_1(id).then((response) => {
        this.form = response.data;
        // console.log(`this.form:${JSON.stringify(this.form)}`)
        this.open = true;
        this.title = "修改退役军人_优抚信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRetirefm_1(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.segment = '4';
            addRetirefm_1(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除该项数据?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRetirefm_1(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有退役军人_优抚信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportRetirefm_1(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
